package com.my.helper.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.helper.R;
import com.my.helper.bean.Stories;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：BiShe
 * 创建人：
 * 创建时间：2018/5/3 1:27
 */

public class ModuleDailyAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Stories> datas = new ArrayList<Stories>();
    private OnDailyClickListener listener;

    public ModuleDailyAdapter(Context context) {
        this.context = context;
        this.datas = new ArrayList<Stories>();
    }

    public void setDatas(List<Stories> datas) {
        this.datas = datas;
    }

    public void setListener(OnDailyClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.module_item_daily, null);
        DailyHolder holder = new DailyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof DailyHolder) {
            ((DailyHolder) holder).tvTitle.setText(datas.get(position).getTitle());

            Picasso.with(context).load(datas.get(position).getImages().get(0))
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .into(((DailyHolder) holder).imageView);

            ((DailyHolder) holder).rlAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClickListener(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class DailyHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tvTitle;
        private RelativeLayout rlAll;

        public DailyHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.module_item_iv_daily);
            tvTitle = (TextView) itemView.findViewById(R.id.module_tv_item_title_daily);
            rlAll = (RelativeLayout) itemView.findViewById(R.id.module_rv_daily_all);
        }
    }

    public interface OnDailyClickListener {
        void onClickListener(int position);
    }
}
